# 列表的推导式/列表生成式
# while循环实现
list1 = []
i = 0
while i < 10:
    list1.append(i)
    i += 1
print(list1)
# for循环实现
list2 = []
for i in range(10):
    list2.append(i)
print(list2)

# 列表推导式实现
list3 = [i for i in range(10)]
print(list3)
# 带if的列表推导式
list4 = [i for i in range(10) if i % 2 == 0]
print(list4)
# 多个for循环实现列表推导式
list5 = [(i, j) for i in range(1, 3) for j in range(0, 3)]
print(list5)

# 字典推导式
# 快速合并列表为字典
list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, "男"]
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)
# 提取字典中的目标数据
counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99}
count1 = {key: value for key, value in counts.items() if value >= 200}
print(count1)

# 集合推导式
list1 = [1, 1, 2]
set1 = {i ** 2 for i in list1}
print(set1)
# 注：列表有去重功能
